import Store from './index'
class ProxyMemory extends Store {
  constructor(opts) {
    super(opts)
    this.reader = {
      type: 'json',
      root: 'data',
      total: 'total'
    }
    if (this.opts.data) {
      this.allData = this.opts.data || []
      this._total = this.allData.length
    }
    if (this.opts.reader) {
      if (this.opts.reader.root) this.reader.root = this.opts.reader.root
      if (this.opts.reader.total) this.reader.total = this.opts.reader.total
    }
    // 通知 created event
    this.emit('created', this)
  }
  read({ start = 1, limit = 10 }) {
    return new Promise((resolve) => {
      const startIndex = (start - 1) * limit
      const endIndex = start * limit - 1
      this._data = this.allData.slice(startIndex || 0, endIndex)
      resolve({ data: this._data, total: this._total })
    })
  }
}

export default ProxyMemory
